from datetime import datetime, timedelta, date

def week_adjust(start_date, end_date):
    start_date = datetime.strptime(start_date, "%Y-%m-%d")
    end_date = datetime.strptime(end_date, "%Y-%m-%d")
    # Adjust the start date
    if start_date.weekday() >= 4:  # Friday or later
        # Move to the next Friday if not already on a Friday
        adjusted_start_date = start_date + timedelta((4 - start_date.weekday()) % 7)
    else:  # Before Friday
        # Move back to the previous Friday
        adjusted_start_date = start_date - timedelta((start_date.weekday() + 3) % 7)
    # Adjust the end date
    if end_date.weekday() >= 3:  # Thursday or later
        # Move to the Thursday of the current week
        adjusted_end_date = end_date - timedelta(end_date.weekday() - 3)
    else:  # Before Thursday
        # Move back to the Thursday of the previous week
        adjusted_end_date = end_date - timedelta(end_date.weekday() + 4)
    # Check if the adjusted dates cover at least one full week
    if adjusted_end_date - adjusted_start_date < timedelta(days=6):
        return "", ""
    else:
        return adjusted_start_date.strftime("%Y-%m-%d"), adjusted_end_date.strftime("%Y-%m-%d")
# Test the function with dates that should not cover a full week



def month_adjust(start_date, end_date):
    # 将输入的字符串日期转换为日期对象
    start = datetime.strptime(start_date, "%Y-%m-%d")
    end = datetime.strptime(end_date, "%Y-%m-%d")

    # 确定开始月份
    if start.day != 1:
        start = start.replace(day=1, month=start.month + 1 if start.month < 12 else 1, year=start.year if start.month < 12 else start.year + 1)
    else:
        start = start.replace(day=1)

    # 确定结束月份
    if end != end.replace(day=1) + timedelta(days=(end.replace(day=1, month=end.month + 1 if end.month < 12 else 1, year=end.year if end.month < 12 else end.year + 1) - end.replace(day=1)).days - 1):
        end = end.replace(day=1, month=end.month - 1 if end.month > 1 else 12, year=end.year if end.month > 1 else end.year - 1)
        end = end + timedelta(days=(end.replace(day=1, month=end.month + 1 if end.month < 12 else 1, year=end.year if end.month < 12 else end.year + 1) - end.replace(day=1)).days - 1)
    else:
        end = end.replace(day=1) + timedelta(days=(end.replace(day=1, month=end.month + 1 if end.month < 12 else 1, year=end.year if end.month < 12 else end.year + 1) - end.replace(day=1)).days - 1)

    # 检查日期范围是否有效
    if start > end:
        return ('', '')  # 没有完整的月份，返回空字符串

    # 格式化输出日期
    start_date_str = start.strftime("%Y-%m-%d")
    end_date_str = end.strftime("%Y-%m-%d")

    return (start_date_str, end_date_str)



def find_largest_complete_years(start_date, end_date):
    # 将输入的字符串日期转换为日期对象
    start = datetime.strptime(start_date, "%Y-%m-%d")
    end = datetime.strptime(end_date, "%Y-%m-%d")

    # 确定开始年份
    if start.month != 1 or start.day != 1:
        start = datetime(start.year + 1, 1, 1)
    else:
        start = datetime(start.year, 1, 1)

    # 确定结束年份
    if end.month != 12 or end.day != 31:
        end = datetime(end.year - 1, 12, 31)
    else:
        end = datetime(end.year, 12, 31)

    # 检查日期范围是否有效
    if start > end:
        return ('', '')  # 没有完整的年份，返回空字符串

    # 格式化输出日期
    start_date_str = start.strftime("%Y-%m-%d")
    end_date_str = end.strftime("%Y-%m-%d")

    return (start_date_str, end_date_str)


# # 示例使用
# print(week_adjust("2023-03-29", "2023-04-01"))
# print(week_adjust("2024-03-30", "2024-04-19"))
# print(week_adjust("2024-03-29", "2024-04-03"))
# print(month_adjust("2023-04-17", "2023-06-25"))
# print(month_adjust("2023-04-01", "2023-06-30"))
# print(find_largest_complete_years("2021-02-15", "2024-11-20"))
# print(find_largest_complete_years("2021-01-01", "2024-12-31"))
